package com.atguigu.dwm.model.controller;

import com.atguigu.dwm.common.bean.QueryInfo;
import com.atguigu.dwm.common.bean.Result;
import com.atguigu.dwm.model.bean.DmModifier;
import com.atguigu.dwm.model.service.DmModifierService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 限定词表 前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/data-model/modifier")
public class DmModifierController {


    @Autowired
    DmModifierService dmModifierService ;



    @GetMapping("/list")
    public Result getList(QueryInfo queryInfo){  //

        List<DmModifier> list = dmModifierService.getQueryList(queryInfo);
        Integer total = dmModifierService.getQueryTotal(queryInfo);
        return Result.ok(list,total);

    }

    @PostMapping("/detail")
    public Result save(@RequestBody DmModifier dmModifier){
        dmModifier.setLastUpdateTime(new Date());
        dmModifier.setLastUpdateUserId(9999L);
        dmModifierService.saveOrUpdate(dmModifier);//保存后 会把主键补充在id上
        return Result.ok(dmModifier);
    }

    @GetMapping("/detail/{id}")
    public Result getDetail(@PathVariable("id") Long id){
        return Result.ok(dmModifierService.getById(id));
    }

    @GetMapping("/options")
    public Result getOptions(@RequestParam(value = "modelId",required = false) Long modelId){
        List<Map<String, Object>> mapList = dmModifierService.listMaps(new QueryWrapper<DmModifier>()
                .eq(modelId!=null , "model_id", modelId)
                .select("id", "name_chn as name", "name_eng as nameEng").eq("is_deleted", "0"));
        return Result.ok(mapList);
    }

}
